package com.kaili.basic.service.impl;

import com.kaili.basic.mapper.DeviceMapper;
import com.kaili.basic.mapper.DeviceCommandMapper;
import com.kaili.basic.mapper.DeviceOnlineMapper;
import com.kaili.basic.pojo.dto.MdmDevicdCommandQueryDto;
import com.kaili.basic.pojo.dto.MdmDeviceCommandAddDto;
import com.kaili.basic.pojo.dto.MdmDeviceCommandDto;
import com.kaili.basic.pojo.entity.MdmDeviceCommand;
import com.kaili.basic.pojo.entity.MdmDeviceOnline;
import com.kaili.basic.pojo.vo.MdmDeviceCommandVo;
import com.kaili.basic.service.IMdmDeviceCommandService;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Desc: 设备命令serviceImpl
 * @author: shenliqiang
 * @Time: 2019/7/26 9:07
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅, 禁止外泄或用于其它商业目的
 */
@Service
@Transactional
public class DeviceCommandServiceImpl implements IMdmDeviceCommandService {
    @Autowired
    private DeviceCommandMapper deviceCommandMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private DeviceOnlineMapper deviceOnlineMapper;

    @Override
    public RestStatus addMdmDeviceCommand(MdmDeviceCommandAddDto dto) {
        MdmDeviceCommand mdmDeviceCommand = new MdmDeviceCommand();
        BeanUtils.copyProperties(dto, mdmDeviceCommand);
        mdmDeviceCommand.setCreateBy(LoginDetailUtils.getCurrent().getId());
        deviceCommandMapper.addMdmDeviceCommand(mdmDeviceCommand);
        return CommonStatus.ADD_OK;
    }

    @Override
    public RestStatus updateMdmDeviceCommandStatus(List<MdmDeviceCommandDto> dtos) {
        //若设备已被删除，则本记录删除标识也改为0（以删除），设备表改回正常时调用update接口修改状态
        for (MdmDeviceCommandDto dto:dtos) {
            if(dto.getImei()==null)
                dto.setImei(this.deviceMapper.getDeviceBySn(dto.getSn()).getImei());
            if(dto.getCleanDevice()==1||dto.getInitializeDevice()==1){
                //设备状态变为'待擦除'
                this.deviceOnlineMapper.updateMdmDeviceOnline(new MdmDeviceOnline(105L,this.deviceMapper.getDeviceByImei(dto.getImei()).getId().longValue()));
            }
            if(dto.getFindDevice()==1){
                //标记‘已启用’
                this.deviceOnlineMapper.updateMdmDeviceOnline(new MdmDeviceOnline(103L,this.deviceMapper.getDeviceByImei(dto.getImei()).getId().longValue()));
            }
            MdmDeviceCommand mdmDeviceCommand = new MdmDeviceCommand();
            BeanUtils.copyProperties(dto, mdmDeviceCommand);
            mdmDeviceCommand.setModifyBy(LoginDetailUtils.getCurrent().getId());
            deviceCommandMapper.updateMdmDeviceCommandStatus(mdmDeviceCommand);
        }
        return CommonStatus.DEVICECOMMAND_CHANGE_SUCCESS;
    }

    @Override
    public RestStatus updateMdmDeviceCommandStatusByDevice(MdmDeviceCommandDto dto) {
        if(dto.getImei()==null)
            dto.setImei(this.deviceMapper.getDeviceBySn(dto.getSn()).getImei());
        MdmDeviceCommand mdmDeviceCommand = new MdmDeviceCommand();
        BeanUtils.copyProperties(dto, mdmDeviceCommand);
//        mdmDeviceCommand.setModifyBy(LoginDetailUtils.getCurrent().getId());
        mdmDeviceCommand.setModifyBy(1);
        deviceCommandMapper.updateMdmDeviceCommandStatus(mdmDeviceCommand);
        return CommonStatus.DEVICECOMMAND_CHANGE_SUCCESS;
    }

    @Override
    public MdmDeviceCommandVo queryMdmDeviceCommand(MdmDevicdCommandQueryDto dto) {
        if(dto.getImei()==null)//如果仅上传了sn，则通过调用deviceMapper获取设备imei
            dto.setImei(deviceMapper.getDeviceBySn(dto.getSn()).getImei());
        return deviceCommandMapper.queryMdmDeviceCommand(dto);
    }
}
